﻿using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using System;
using OnlineBookReader;
using System.Net;
using HtmlExtractor;
using Reader.Domain;
using System.Linq;
using SLHtmlExtractor.ObjectOutput;
using System.Windows.Markup;
using System.Windows.Navigation;
using System.IO;
using System.Collections.Generic;

namespace Reader.View
{
    [Sample("排行榜", "CatalogView")]
    public partial class TopView : UserControl
    {
      // public override on
        public TopView()
        {
            InitializeComponent();
        }
        public string Uri { get; set; }
        public TopView(string uri)
            : this()
        {
            Uri = uri;
        }
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                WebClient wc = new WebClient();
                wc.Encoding = new Gb2312Encoding();
                wc.DownloadStringAsync(new Uri(Uri));
                wc.DownloadStringCompleted += (s, args) =>
                {
                    if (args.Error == null)
                    {
                        ParserContext context = new ParserContext(args.Result, Uri);
                        string extractorFile = @"Resource/ObjectExtractor.config";
                        Extractor extr = new Extractor(extractorFile);
                        PageResult result;
                        extr.TryGetResult(context, out result);
                        object b = new Tops();
                        ConvertToObject ct = new ConvertToObject();
                        ct.TryGetOutPutResult(result, ref b);
                        Tops bp = (Tops)b;
                        topGrid.DataContext = bp.OrderTopRates;
                    }
                };
            }
            catch
            { }
        }
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hb = e.OriginalSource as HyperlinkButton;
            SampleBrowser.Current.PopulateSamplesTab("bookpage", hb.CommandParameter.ToString());
        }

        private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hb = e.OriginalSource as HyperlinkButton;
            SampleBrowser.Current.PopulateSamplesTab("chapter", hb.CommandParameter.ToString());
        }

        private void topGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            List<TopRate> tops = (sender as DataGrid).DataContext as List<TopRate>;
            if (tops != null && tops.Count > 0)
            {
                int index = e.Row.GetIndex();
                TopRate top=tops[index];
              
                if (QidianUtil.IsVipUri(top.LastChapterUri))
                {
                    HyperlinkButton hb= topGrid.Columns[3].GetCellContent(e.Row) as HyperlinkButton;
                    hb.IsEnabled = false;
                    //var newColumn = new DataGridTextColumn() { Header = "Vip章节" };
                    //newColumn.Binding = new System.Windows.Data.Binding("LastChapterName");
                    //topGrid.Columns.RemoveAt(3);
                    //topGrid.Columns.Insert(3, newColumn);
                   // topGrid.Columns[4].Visibility = Visibility.Collapsed;//普通章节
                    //topGrid.Columns[3].Visibility = Visibility.Visible;                    
                }
                //else
                //{
                //    topGrid.Columns[4].Visibility = Visibility.Visible;
                //    //topGrid.Columns[3].Visibility = Visibility.Collapsed;
                //}
            }
            
        }

        private void topGrid_Loaded(object sender, RoutedEventArgs e)
        {
          
           
        }
    }
}
